﻿Imports System.Data.SqlClient
Public Class employeeinfo
    Dim cmd As New SqlCommand

#Region "Code search and refresh Data"
    Public Sub refreshdata()
        grdemployee.Rows.Clear()
        cmd.Connection = cn
        cmd.CommandText = "select * from tblemployee where employee_Name like N'%" & txtsearch.Text & "%' OR employee_ID like N'%" & txtsearch.Text & "%'"
        Dim rd As SqlDataReader = cmd.ExecuteReader
        While rd.Read = True
            Me.grdemployee.Rows.Add(rd(0), rd(1), rd(2), rd(3), rd(4), rd(5), rd(6), rd(7), rd(8))
        End While
        rd.Close()
        radname.Checked = True
    End Sub
#End Region

#Region "Open Form Employee editor"

    Private Sub btnadd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnadd.Click
        Dim nfrm As New Employeeeditor
        If nfrm.ShowDialog() = Windows.Forms.DialogResult.OK Then
            refreshdata()
        End If

    End Sub
#End Region

#Region "Connection"
    Private Sub employeeinfo_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        condb()
        refreshdata()
        Main.lblheader.Text = "ពត៌មានបុគ្គលិក"
    End Sub
#End Region

#Region "For update Data"
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnupdate.Click
        Try
            If Me.grdemployee.SelectedRows.Count = 0 Then
                Exit Sub
            End If
            Dim row As DataGridViewRow = Me.grdemployee.SelectedRows(0)
            Dim frm As New employeeupdate
            frm.txtid.Text = row.Cells("id").Value
            frm.txtname.Text = row.Cells("empname").Value
            frm.txtgender.Text = row.Cells("gender").Value
            frm.txtdob.Text = row.Cells("dob").Value
            frm.txtpob.Text = row.Cells("pob").Value
            frm.txtphon.Text = row.Cells("Phone").Value
            frm.txtstartwork.Text = row.Cells("Startwork").Value
            frm.txtsalary.Text = row.Cells("salary").Value
            frm.txtposition.Text = row.Cells("Possition").Value
            frm.ShowDialog()
            refreshdata()
        Catch ex As Exception
        End Try
    End Sub
#End Region

#Region "Search TexBox"

    Private Sub txtsearch_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtsearch.TextChanged
        refreshdata()
    End Sub
#End Region

#Region "Delete data"
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Try
            If Me.grdemployee.SelectedRows.Count = 0 Then
                Exit Sub
            End If
            If MessageBox.Show("Are you want to delete", "Delete Confirme", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.No Then
                Exit Sub
            End If
            For Each row In grdemployee.SelectedRows
                Dim id = row.Cells("id").Value
                cmd.Connection = cn
                cmd.CommandText = "delete from tblemployee where Employee_Id= " & id
                cmd.ExecuteNonQuery()
            Next
            refreshdata()
        Catch ex As Exception
        End Try

    End Sub
#End Region

End Class